import xlrd
import os
import sys
import utils
import tsConfig
buildModel = "ts" # 输出文件格式
csvFileList = []
def buildExceil():
    global csvFileList
    local_dir = "../config/inputs"
    fileList = os.listdir(local_dir)  # 列出文件夹下所有的目录与文件
    for i in range(0, len(fileList)):
        filename = fileList[i]
        path = os.path.join(local_dir, filename)
        print("##########",path)
        if os.path.isfile(path):
            fileStrArr = os.path.splitext(filename)
            if (fileStrArr[1] == ".xlsx" or fileStrArr[1] == ".xlsm") and fileStrArr[0].find("~") == -1:
                csvFileList.append(fileStrArr[0])
    print("file listy = ",csvFileList)
    for i in range(0, len(fileList)):
        filename = fileList[i]
        path = os.path.join(local_dir, filename)
        if os.path.isfile(path):
            fileStrArr = os.path.splitext(filename)
            if (fileStrArr[1] == ".xlsx" or fileStrArr[1] == ".xlsm") and fileStrArr[0].find("~") == -1:
                parseSingleExceil(path)
def parseSingleExceil(filePath):
    global csvFileList
    excelFile = xlrd.open_workbook(filePath)
    sheet:xlrd.sheet.Sheet= excelFile.sheet_by_index(0)
    xNum = sheet.ncols
    yNum = sheet.nrows
    
   
    curY = 0

    valueNames = []
    valueTypes = []
    while curY <= yNum:
        valueName = str(sheet.cell_value(0,curY))
        valueType = str(sheet.cell_value(1,curY))
        valueNames.append(valueName)
        valueTypes.append(valueType)
        curY += 1

    curY = 2
   
    textValues = ""
    while curY < yNum:
        curX = 0
        while curX < xNum:
            valueName = valueNames[curX]
            valueType = valueTypes[curX]
            value = sheet.cell_value(curY,curX)
            #print(value)
            if valueType == "number":
    
                try:
                    value = int(value)
                except :
                    print("can convet to num {}".format(value))
                    os.system("pause")
            elif valueType == "string":
                value = str(value)
            elif  valueType == "sarray":
                #检查数据合法性
                try:
                    s = value.split(",")
                except:
                    print("can't convert sarray {}".format(value))
                    os.system("pause")
                
            elif  valueType == "iarray":
                #检查数据合法性
                try:
                    s = value.split(",")
                    for sValue in s:
                       int(sValue)
                except:
                    print("can't convert iArray {}".format(value))
                    os.system("pause")
            else:
                print("找不到的valueType{}".format(valueType))
                os.system("pause")
            if curX < xNum -1:
                textValues += str(value) + "&"
            else:
                textValues += str(value) + "\n"
            curX += 1
        curY += 1

    # ____write text_____
    textContent = ""
    index = 0
    while index < len(valueNames):
        valueName =  valueNames[index]
        if index < len(valueNames) - 1:
            textContent += valueName + "&"
        else:
            textContent += valueName + "\n"
        index += 1
    index = 0
    while index < len(valueTypes):
        valueType=  valueTypes[index]
        if index < len(valueTypes) - 1:
            textContent += valueType + "&"
        else:
            textContent += valueType + "\n"
        index += 1
    textContent += textValues
    textFilePath = "../config/outpus/bin/{}.txt".format(utils.getFileName(filePath))
    utils.writeFile(textFilePath,textContent)
    tsConfig.writeTypeFile(filePath)
    tsConfig.WriteCsv(csvFileList)
if __name__ == "__main__":
    buildExceil()